<template>
  <div class="user-profile-container">
    <!-- 顶部用户信息卡片 -->
        <!-- 顶部用户信息卡片 -->
    <div class="profile-card">
      <div class="profile-banner animated-gradient"></div>
      <div class="profile-content">
        <el-avatar :size="120" :src="defaultAvatar" class="profile-avatar" />
        <div class="profile-info">
          <h1 class="username">{{ userData.userName }}</h1>
          <div class="user-meta">
            <el-tag :type="userData.gender === 0 ? 'primary' : 'danger'" effect="dark" round>
              {{ userData.gender === 0 ? '男' : '女' }}
            </el-tag>
            <span class="register-date">注册时间 {{ formatDate(userData.createDate) }}</span>
          </div>
          <p class="user-bio">{{ userData.bio || '这个人很懒，还没有写自我介绍~' }}</p>
        </div>
      </div>
    </div>

    <!-- 数据统计 -->
    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-value">{{ userData.followerId || 0 }}</div>
        <div class="stat-label">粉丝</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ userData.followeeId || 0 }}</div>
        <div class="stat-label">关注</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ userData.newsFavorites || 0 }}</div>
        <div class="stat-label">收藏</div>
      </div>
    </div>

    <!-- 作品展示 -->
    <div class="section-title">
      <el-icon><Collection /></el-icon>
      <span>个人作品</span>
    </div>
    
   <div v-if="works.length > 0" class="works-grid">
        <router-link 
            v-for="work in works" 
            :key="work.id" 
            :to="'/news/' + work.id"
            class="work-card-wrapper"
        >
            <div class="work-card">
            <div class="work-cover" :style="{ backgroundImage: `url(${work.picPath || '/src/assets/images/wZhi.jpeg'})` }"></div>
            <div class="work-content">
                <h3>{{ work.title }}</h3>
                <div class="work-footer">
                <span class="work-date">{{ formatDate(work.createDate) }}</span>
                <span class="work-date">作者：{{ work.userName }}</span>
                </div>
            </div>
            </div>
        </router-link>
        </div>
        <el-empty v-else description="暂无作品" :image-size="200" />
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { 
  Collection, StarFilled, Message, Iphone, 
  ChatDotRound, UserFilled, Edit 
} from '@element-plus/icons-vue'
import axios from 'axios'
import defaultAvatar from '@/assets/images/doro.jpg'

const route = useRoute()
const router = useRouter()

// 从路由参数获取用户ID
const userId = route.params.id
const currentUserId = localStorage.getItem('userId')

const isCurrentUser = computed(() => {
  return userId == currentUserId
})

const userData = ref({
  // 初始化数据...
})

const works = ref([])
const loading = ref(true)


// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}
//获取个人信息
const fetchUserData = async () => {
  try {
    loading.value = true
    const response = await axios.get(`/api/auth/personalCenter?id=${userId}`)
    if (response.data.code === 200) {
      userData.value = response.data.data
      await fetchUserWorks()
    } else {
      ElMessage.error(response.data.message || '获取用户信息失败')
    }
  } catch (error) {
    console.error('获取用户数据失败:', error)
    ElMessage.error('加载用户信息失败')
    // 可以在这里添加重定向逻辑，例如：
    // router.push('/not-found')
  } finally {
    loading.value = false
  }
}

//获取个人作品
// 获取用户作品
const fetchUserWorks = async () => {
  try {
    const response = await axios.get(`/api/news/getNewsByName?authorId=${userId}`)
    if (response.data.code === 200) {
      works.value = response.data.data || []
    } else {
      ElMessage.error(response.data.message || '获取作品列表失败')
    }
  } catch (error) {
    console.error('获取用户作品失败:', error)
    ElMessage.error('加载作品列表失败')
  }
}

onMounted(() => {
  fetchUserData(),
  fetchUserWorks()
})
</script>

<style scoped>
.user-profile-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #ffffff;
}

/* 用户卡片 */
.profile-card {
  position: relative;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 24px;
}

.profile-banner {
  height: 120px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.profile-content {
  display: flex;
  padding: 0 30px 30px;
  margin-top: -60px;
  position: relative;
  z-index: 2;
}

.profile-avatar {
  border: 4px solid white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.profile-info {
  margin-left: 30px;
  flex: 1;
}

.username {
  margin: 10px 0 5px;
  font-size: 28px;
  font-weight: 600;
  color: #333;
}

.user-meta {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 15px;
}

.register-date {
  color: #666;
  font-size: 14px;
}

.user-bio {
  color: #1f1e1e;
  line-height: 1.6;
  margin: 0;
  max-width: 600px;
}

/* 数据统计 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 40px;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-value {
  font-size: 32px;
  font-weight: 700;
  color: #333;
  margin-bottom: 5px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

/* 作品展示 */
.section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 40px 0 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.works-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 25px;
}

.work-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s;
}

.work-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}

.work-cover {
  height: 180px;
  background-size: cover;
  background-position: center;
}

.work-content {
  padding: 20px;
}

.work-content h3 {
  margin: 0 0 10px;
  font-size: 18px;
  color: #333;
}

.work-desc {
  color: #666;
  line-height: 1.6;
  margin: 0 0 15px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.work-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: #888;
}

.work-stats {
  display: flex;
  align-items: center;
  gap: 15px;
}

.likes {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #ff6b6b;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-content {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding-bottom: 20px;
  }
  
  .profile-info {
    margin-left: 0;
    margin-top: 15px;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .works-grid {
    grid-template-columns: 1fr;
  }
}
.profile-banner.animated-gradient {
  height: 180px; /* 增加高度 */
  background: linear-gradient(135deg, #1e90ff, #00bfff, #87cefa, #1e90ff);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
  position: relative;
  overflow: hidden;
}

/* 添加一些装饰元素 */
.profile-banner.animated-gradient::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 30%, rgba(255,255,255,0.1) 0%, transparent 50%);
}

.profile-banner.animated-gradient::after {
  content: "";
  position: absolute;
  bottom: -50px;
  right: -50px;
  width: 150px;
  height: 150px;
  background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
  border-radius: 50%;
}

/* 背景动画 */
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* 调整头像位置 */
.profile-content {
  margin-top: -90px; /* 因为banner高度增加了 */
}

/* 调整用户名颜色使其在深色背景上更突出 */
.username {
  color: white;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.user-meta {
  color: rgba(255,255,255,0.9);
}

.register-date {
  color: rgba(255,255,255,0.8);
}

/* .user-bio {
  color: rgba(255,255,255,0.9);
  max-width: 600px;
} */

/* 其他样式保持不变 */
.user-profile-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #ffffff;
}

.profile-card {
  position: relative;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 24px;
}

.profile-avatar {
  border: 4px solid white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  z-index: 2;
}

.profile-info {
  margin-left: 30px;
  flex: 1;
  position: relative;
  z-index: 2;
}
</style>